#!/usr/bin/env python

import collections
import glob
import optparse
import os

from charmicat import BadCommandLineError

def main():
    '''
    ypipi_batch_append.py [opts] path/

    Search `path/' for h5 files, and append them into single files according to their
    file names.
    '''

    op = optparse.OptionParser(usage=main.__doc__)

    op.add_option("-j", "--job-name", dest='job_name', default=None,
                  help='Name jobs NAME in SLURM queue', 
                  metavar='NAME')
    op.add_option("-o", "--outdir", dest='outdir', default=None,
                  help='Leave output files in DIR', metavar='DIR')
    op.add_option("-t", "--duration", dest='duration', 
                  default='08:00:00',
                  help='Set job duration of N minutes or HH:MM:SS',
                  metavar='N (or) HH:MM:SS')
    op.add_option("--nodes", dest='nnodes', type='int', default=1,
                  help='Use N nodes per job', metavar='N')
    op.add_option('-m', '--module-path', dest='module_path', default=None,
                  help='Add PATH to the LD_LIBRARY_PATH for the job',
                  metavar='PATH')
    op.add_option("--stdout", dest='stdout', default=None,
                  help='Redirect job stdout to PATH', metavar='PATH')
    op.add_option("--stderr", dest='stderr', default=None,
                  help='Redirect job stderr to PATH', metavar='PATH')
    op.add_option("--stdout-dir", dest='stdoutdir', default=None,
                  help='Place job stdout in DIR', metavar='DIR')
    op.add_option("--stderr-dir", dest='stderrdir', default=None,
                  help='Place job stderr in DIR', metavar='DIR')

    opts, args = op.parse_args()

    if len(args) < 1:
        raise BadCommandLineError('Please supply a directory containing PyTables files', -1)

    path_dict = collections.defaultdict(list)
    for path in glob.iglob(os.path.join(args[0], '*.h5')):
        tokens = path.split('-')
        tag = tokens[1]
        stream = tokens[2]
        
        exp = int(tokens[4].split('r')[0][1:])
        
        key = "generic_mc_exp{0}s{1}_{2}.h5".format(exp, stream, tag)

        path_dict[key].append(path)


    for key in path_dict:
        with open(key.rsplit('.', 1)[0] + '.1x.sh', 'w') as fp:
            cmdline = 'ptappend.py nt {0} '.format(opts.output, key)
            cmdline += ' '.join(path_dict[key])
            fp.write(SlurmScript(cmdline, **vars(opts)))

        


if __name__ == "__main__":
    try:
        sys.exit(main())
    except BadCommandLineError, e:
        import os
        sys.stderr.write(str(e) + "\n")
        sys.stderr.write("See `{0} -h' for more.\n".format(os.path.basename(sys.argv[0])))
        sys.stderr.flush()
        sys.exit(e.retcode)
